package com.ruoyi.process.pipemaintainence.service.impl;


import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.process.pipemaintainence.domain.BpmnCopyProcess;
import com.ruoyi.process.pipemaintainence.mapper.BpmnCopyProcessMapper;
import com.ruoyi.process.pipemaintainence.provider.BpmnBusinessProvider;
import com.ruoyi.process.pipemaintainence.service.BpmnApprovalService;
import com.ruoyi.process.pipemaintainence.service.BpmnCopyProcessService;
import com.ruoyi.process.pipemaintainence.service.IDrainagePipeMainService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BpmnCopyProcessServiceImpl implements BpmnCopyProcessService {

    @Autowired
    private BpmnCopyProcessMapper bpmnCopyProcessMapper;

    @Autowired
    private BpmnBusinessProvider bpmnBusinessProvider;

    @Autowired
    private IDrainagePipeMainService drainagePipeMainService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private HistoryService historyService;

    @Override
    public int deleteByPrimaryKey(String id) {
        return bpmnCopyProcessMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BpmnCopyProcess record) {
        return bpmnCopyProcessMapper.insert(record);
    }

    @Override
    public int insertSelective(BpmnCopyProcess record) {
        return bpmnCopyProcessMapper.insertSelective(record);
    }

    @Override
    public BpmnCopyProcess selectByPrimaryKey(String id) {
        return bpmnCopyProcessMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BpmnCopyProcess record) {
        return bpmnCopyProcessMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BpmnCopyProcess record) {
        return bpmnCopyProcessMapper.updateByPrimaryKey(record);
    }

    @Override
    public void batchInsert(List<BpmnCopyProcess> list) {
        if (BeanUtils.isNotEmpty(list)) {
            for (BpmnCopyProcess bpmnCopyProcess : list) {
                bpmnCopyProcessMapper.insert(bpmnCopyProcess);
            }
        }
    }

    @Override
    public List<BpmnCopyProcess> findByUserId(String userId) {
        return bpmnCopyProcessMapper.findByUserId(userId);
    }

    @Override
    public List<String> findReceiverByProceInstanceId(String processInstanceId) {
        return bpmnCopyProcessMapper.findReceiverByProceInstanceId(processInstanceId);
    }

    @Override
    public List<Map<String, Object>> findCopyProcessByUserId(Integer pageNo, Integer pageSize, String userId) {
        //分页  抄送的流程
        PageHelper.startPage(pageNo, pageSize);
        List<BpmnCopyProcess> processList = findByUserId(userId);
        //查询发起人 、 封装各个节点的信息
        List<Map<String, Object>> processListMap = findBpmnCopyProcessDetail(processList);
        return processListMap;
    }

    /**
     * 查询详情数据
     *
     * @param processList
     * @return
     */
    private List<Map<String, Object>> findBpmnCopyProcessDetail(List<BpmnCopyProcess> processList) {
        List<Map<String, Object>> listMap = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (BeanUtils.isNotEmpty(processList)) {
            for (BpmnCopyProcess receiverProcess : processList) {
                String processInstanceId = receiverProcess.getProcessInstanceId();
                if (BeanUtils.isEmpty(processInstanceId)) continue;
                HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).orderByProcessInstanceStartTime().asc().singleResult();
                if (BeanUtils.isNotEmpty(processInstance)) {
                    Map<String, Object> values = new HashMap<>();
                    values.put("processInstance", processInstance);
                    //发起人
                    String startUserId = drainagePipeMainService.getStartedByProcessInstanceId(processInstanceId);
                    SysUser sysUser = userService.selectUserById(Long.valueOf(startUserId));
                    values.put("startByUser", sysUser);
                    //创建时间
                    values.put("createTime", sdf.format(receiverProcess.getCreateTime()));
                    values.put("receiverProcess", receiverProcess);
                    values.put("businessKey", receiverProcess.getBussinessKey());
                    values.put("startedByUserId", sysUser.getUserId());
                    values.put("startedByUserName", sysUser.getUserName());
                    values.put("processInstanceId", processInstanceId);

                    //各个已审批流程的信息
                    List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list();
                    //封装各个节点的信息
                    List<HashMap<String, Object>> taskDetailList = bpmnBusinessProvider.getTaskDetail(list);
                    values.put("taskDetailList", taskDetailList);
                    listMap.add(values);
                }
            }
        }
        return listMap;
    }

    @Override
    public List<BpmnCopyProcess> findByBusinessKey(String businessKey) {
        return bpmnCopyProcessMapper.findByBusinessKey(businessKey);
    }
}